package com.example.springbootaxiosexample.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.springbootaxiosexample.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/api")
@Slf4j
public class APIController {

    @GetMapping(value = "/user",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> getUser(@RequestParam("id") final Integer id) throws Exception {
        if (id > 0) {
            return new ResponseEntity<>("OK", HttpStatus.OK);
        }else{
            throw new Exception("id必须大于0");
        }
    }

    @PostMapping(value = "/user",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<User> saveUser(@RequestBody User user){
        System.err.println(JSONObject.toJSONString(user,true));
        return new ResponseEntity<>(user,HttpStatus.OK);
    }
}
